<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="Pandas的用法的总结">
<meta name="keywords" content="学习笔记,Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Pandas学习笔记01">
<meta property="og:url" content="https:&#x2F;&#x2F;fwj1635387072.github.io&#x2F;2021&#x2F;03&#x2F;14&#x2F;pandas&#x2F;index.html">
<meta property="og:site_name" content="付文杰的博客">
<meta property="og:description" content="Pandas的用法的总结">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2021-03-27T01:46:19.837Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://fwj1635387072.github.io/2021/03/14/pandas/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>


  <title>Pandas学习笔记01 | 付文杰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">付文杰的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">个人博客  |  日常</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>

      </div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://FWj1635387072.github.io/2021/03/14/pandas/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/headImage.jpg">
      <meta itemprop="name" content="付文杰">
      <meta itemprop="description" content="兰州交通大学 | 本科生 <br> 信息管理与信息系统">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="付文杰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Pandas学习笔记01
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-14 22:49:13" itemprop="dateCreated datePublished" datetime="2021-03-14T22:49:13+08:00">2021-03-14</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-03-27 09:46:19" itemprop="dateModified" datetime="2021-03-27T09:46:19+08:00">2021-03-27</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index">
                    <span itemprop="name">Python</span>
                  </a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.6k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>11 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <pre><code>Pandas的用法的总结</code></pre><a id="more"></a>



<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line">food_info = pd.read_csv(<span class="string">"food_info.csv"</span>)</span><br><span class="line">print(food_info.dtypes)</span><br><span class="line"></span><br><span class="line">out:</span><br><span class="line">NDB_No               int64</span><br><span class="line">Shrt_Desc           object</span><br><span class="line">Water_(g)          float64</span><br><span class="line">Energ_Kcal           int64</span><br><span class="line">dtype: object</span><br></pre></td></tr></table></figure>

<p>得到前5行数据</p>
<p><code>head(n)</code>   n默认为5</p>
<p><code>column</code>得到列名</p>
<p><code>shape</code>得到维数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">first_rows = food_info.head()</span><br><span class="line">print(first_rows)</span><br><span class="line">print(food_info.columns)</span><br><span class="line">print(food_info.shape)</span><br></pre></td></tr></table></figure>

<p><code>loc[]</code>得到指定的行，可以使用切片,也可以传入list得到指定的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">print(food_info.loc[<span class="number">0</span>])</span><br><span class="line">print(food_info.loc[<span class="number">1</span>:<span class="number">4</span>])</span><br><span class="line"></span><br><span class="line">two_five_ten = [<span class="number">2</span>,<span class="number">5</span>,<span class="number">10</span>]</span><br><span class="line">print(food_info.loc[two_five_ten])</span><br><span class="line">print(food_info.loc[[<span class="number">2</span>,<span class="number">5</span>,<span class="number">10</span>]])</span><br></pre></td></tr></table></figure>

<p>直接获得某行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">print(food_info[<span class="string">"NDB_No"</span>])</span><br><span class="line"></span><br><span class="line">columns = [<span class="string">"Zinc_(mg)"</span>,<span class="string">"Copper_(mg)"</span>]</span><br><span class="line">print(food_info[columns])</span><br><span class="line">print(food_info[[<span class="string">"Zinc_(mg)"</span>,<span class="string">"Copper_(mg)"</span>]])</span><br></pre></td></tr></table></figure>

<p>案例：获得后缀为”(g)”的所有行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">col_name = food_info.columns.tolist()</span><br><span class="line">gram_column = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> cn <span class="keyword">in</span> col_name:</span><br><span class="line">    <span class="keyword">if</span> cn.endswith(<span class="string">"(g)"</span>):</span><br><span class="line">        gram_column.append(cn)</span><br><span class="line">gram_column = food_info[gram_column]</span><br><span class="line">print(gram_column)</span><br></pre></td></tr></table></figure>

<p>对某列直接进行运算</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">water_energy = food_info[<span class="string">"Water_(g)"</span>] * food_info[<span class="string">"Energ_Kcal"</span>]</span><br><span class="line">iron_grams = food_info[<span class="string">"Iron_(mg)"</span>] / <span class="number">1000</span></span><br><span class="line">food_info[<span class="string">"Iron_(g)"</span>] = iron_grams</span><br><span class="line">print(food_info.columns)</span><br><span class="line"><span class="comment">#如果没有某列，会直接在后面加上</span></span><br></pre></td></tr></table></figure>

<p>获取某列最大的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">max_calories = food_info[<span class="string">"Energ_Kcal"</span>].max()</span><br><span class="line">print(max_calories)</span><br></pre></td></tr></table></figure>

<p>对某列进行排序 <code>sort_values()</code></p>
<p><code>inplace</code>：是否替代，否，将创建另外的对象存储</p>
<p><code>ascending</code>：默认True，升序</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">food_info.sort_values(<span class="string">"Sodium_(mg)"</span>,inplace = <span class="literal">True</span>)</span><br><span class="line">print(food_info[<span class="string">"Sodium_(mg)"</span>])</span><br><span class="line">food_info.sort_values(<span class="string">"Sodium_(mg)"</span>,inplace=<span class="literal">True</span>,ascending=<span class="literal">False</span>)</span><br><span class="line">print(food_info[<span class="string">"Sodium_(mg)"</span>])</span><br></pre></td></tr></table></figure>



<hr>
<p>案例：泰坦尼克号</p>
<p>数据读取</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line">titanic_survival = pd.read_csv(<span class="string">"titanic_train.csv"</span>)</span><br><span class="line">titanic_survival.head()</span><br></pre></td></tr></table></figure>

<p>计算每列的空值个数（自定义函数）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">not_null_count</span><span class="params">(column)</span>:</span></span><br><span class="line">    column_null = pd.isnull(column)</span><br><span class="line">    null = column[column_null]</span><br><span class="line">    <span class="keyword">return</span> len(null)</span><br><span class="line"></span><br><span class="line">column_null_count = titanic_survival.apply(not_null_count)</span><br><span class="line">print(column_null_count)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#----------------------------------</span></span><br><span class="line">age = titanic_survival[<span class="string">"Age"</span>]</span><br><span class="line">age_is_null = pd.isnull(age)</span><br><span class="line">age_null_count = len(age[age_is_null])</span><br><span class="line">print(age_null_count)</span><br></pre></td></tr></table></figure>

<p>计算平均年龄</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">good_age = titanic_survival[<span class="string">"Age"</span>][age_is_null == <span class="literal">False</span>]</span><br><span class="line">print(good_age)</span><br><span class="line">mean_age = good_age.mean()</span><br><span class="line">print(mean_age)</span><br></pre></td></tr></table></figure>

<p>删除某列（某几列）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">titanic_survival.drop([<span class="string">"Name"</span>],axis=<span class="number">1</span>)</span><br></pre></td></tr></table></figure>

<p>计算各个等级舱的平均船票价格</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#类似于聚类</span></span><br><span class="line">pclass = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">fares_by_class = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> pclass:</span><br><span class="line">    row_class = titanic_survival[titanic_survival[<span class="string">"Pclass"</span>] == i]</span><br><span class="line">    fares_class = row_class[<span class="string">"Fare"</span>]</span><br><span class="line">    fares_by_class[i] = fares_class.mean()</span><br><span class="line">print(fares_by_class)</span><br><span class="line"></span><br><span class="line"><span class="comment">#也可以使用透视表</span></span><br><span class="line">passage_fare_by_class = titanic_survival.pivot_table(index=<span class="string">"Pclass"</span>,values=<span class="string">"Fare"</span>,aggfunc=np.mean)</span><br><span class="line">print(passage_fare_by_class)</span><br></pre></td></tr></table></figure>

<p>透视图<code>piovt_table</code></p>
<p>计算每个等级船舱获救比例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">passage_survival_age = titanic_survival.pivot_table(index=<span class="string">"Pclass"</span>,values=<span class="string">"Survived"</span>,aggfunc=np.mean)</span><br><span class="line">print(passage_survival_age)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#计算有多少个上船地点，在某个上船地点的船费是多少</span></span><br><span class="line">port_stats = titanic_survival.pivot_table(index=<span class="string">"Embarked"</span>,values=[<span class="string">"Fare"</span>,<span class="string">"Survived"</span>],aggfunc=np.sum)</span><br><span class="line">print(port_stats)</span><br></pre></td></tr></table></figure>





<p>缺失值处理。。。。。。。</p>
<p><code>dropna</code> <code>fillna</code> ‘’ </p>
<hr>
<p><code>loc</code>获取指定元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">row_index_83_age = titanic_survival.loc[<span class="number">83</span>,<span class="string">"Age"</span>]</span><br><span class="line">row_index_1000_pclass = titanic_survival.loc[<span class="number">766</span>,<span class="string">"Pclass"</span>]</span><br><span class="line">print(row_index_1000_pclass)</span><br><span class="line">print(row_index_83_age)</span><br></pre></td></tr></table></figure>

<p>处理数据后将index重置<code>reset_index()</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">new_titanic_survival = titanic_survival.sort_values(<span class="string">"Age"</span>,ascending=<span class="literal">False</span>)</span><br><span class="line">print(new_titanic_survival)</span><br><span class="line">titanic_reindexed = new_titanic_survival.reset_index(drop=<span class="literal">True</span>)</span><br><span class="line">print(titanic_reindexed.iloc[<span class="number">0</span>:<span class="number">10</span>])</span><br></pre></td></tr></table></figure>

<p>自定义函数：输出第100行元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hundred_row</span><span class="params">(column)</span>:</span></span><br><span class="line">    hundred_item = column.iloc[<span class="number">99</span>]</span><br><span class="line">    <span class="keyword">return</span> hundred_item</span><br><span class="line"></span><br><span class="line">hundred_row = titanic_survival.apply(hundred_row)</span><br><span class="line">print(hundred_row)</span><br></pre></td></tr></table></figure>

<p>将Class列按基数打印,</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">which_class</span><span class="params">(row)</span>:</span></span><br><span class="line">    pclass = row[<span class="string">'Pclass'</span>]</span><br><span class="line">    <span class="keyword">if</span> pd.isnull(pclass):</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Unknown"</span></span><br><span class="line">    <span class="keyword">elif</span> pclass == <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"First Class"</span></span><br><span class="line">    <span class="keyword">elif</span> pclass == <span class="number">2</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Second Class"</span></span><br><span class="line">    <span class="keyword">elif</span> pclass == <span class="number">3</span>:</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Third Class"</span></span><br><span class="line">    </span><br><span class="line">classes = titanic_survival.apply(which_class,axis = <span class="number">1</span>)</span><br><span class="line">print(classes)</span><br></pre></td></tr></table></figure>





<hr>
<h1 id="Python基础部分"><a href="#Python基础部分" class="headerlink" title="Python基础部分"></a>Python基础部分</h1><h2 id="列表推导式和条件赋值"><a href="#列表推导式和条件赋值" class="headerlink" title="列表推导式和条件赋值"></a>列表推导式和条件赋值</h2><p>生成数字序列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">L = []</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">my_func</span><span class="params">(x)</span>:</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">2</span>*x</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">    L.append(my_func(i))</span><br><span class="line">L</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">L = [my_func(i) <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>)]</span><br><span class="line">print(L)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">L = [m + <span class="string">'_'</span> + n <span class="keyword">for</span> m <span class="keyword">in</span> [<span class="string">'a'</span>,<span class="string">'b'</span>] <span class="keyword">for</span> n <span class="keyword">in</span> [<span class="string">'c'</span>,<span class="string">'d'</span>]]</span><br><span class="line">print(L)</span><br></pre></td></tr></table></figure>

<p>语法糖带有if选择的条件赋值<code>value = A if condition else b</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">value = <span class="string">'cat'</span> <span class="keyword">if</span> <span class="number">2</span> &gt; <span class="number">1</span> <span class="keyword">else</span> <span class="string">'dog'</span></span><br><span class="line">print(value)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#将列表中超过5的元素用5替换</span></span><br><span class="line">L = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>]</span><br><span class="line">L = [i <span class="keyword">if</span> i &lt; <span class="number">5</span> <span class="keyword">else</span> <span class="number">5</span> <span class="keyword">for</span> i <span class="keyword">in</span> L]</span><br><span class="line">print(L)</span><br></pre></td></tr></table></figure>

<h2 id="匿名函数和map方法"><a href="#匿名函数和map方法" class="headerlink" title="匿名函数和map方法"></a>匿名函数和map方法</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">my_func = <span class="keyword">lambda</span> x : <span class="number">2</span>*x</span><br><span class="line">my_func(<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">multi_para_func = <span class="keyword">lambda</span> a,b : a + b</span><br><span class="line">multi_para_func(<span class="number">10</span>,<span class="number">11</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[(<span class="keyword">lambda</span> x : <span class="number">2</span>*x)(i) <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>)]</span><br></pre></td></tr></table></figure>

<p>对于列表推导式的匿名函数映射，可用map函数完成，返回map对象，用list转换。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list(map(<span class="keyword">lambda</span> x: <span class="number">2</span>*x,range(<span class="number">5</span>)))</span><br></pre></td></tr></table></figure>

<p>多值输入</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list(map(<span class="keyword">lambda</span> x,y: str(x)+<span class="string">'_'</span>+y , range(<span class="number">5</span>),list(<span class="string">'abcde'</span>)))</span><br></pre></td></tr></table></figure>

<h2 id="zip对象和enumerate方法"><a href="#zip对象和enumerate方法" class="headerlink" title="zip对象和enumerate方法"></a>zip对象和enumerate方法</h2><p>zip可将多个可迭代对象打包成一个元组构成的可迭代对象，返回一个zip对象，通过tuple，list可以得到相应的打包结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">L1,L2,L3 = list(<span class="string">'abc'</span>),list(<span class="string">'def'</span>),list(<span class="string">'hij'</span>)</span><br><span class="line">list(zip(L1,L2,L3))</span><br><span class="line">tuple(zip(L1,L2,L3))</span><br><span class="line">out：</span><br><span class="line">[(<span class="string">'a'</span>, <span class="string">'d'</span>, <span class="string">'h'</span>), (<span class="string">'b'</span>, <span class="string">'e'</span>, <span class="string">'i'</span>), (<span class="string">'c'</span>, <span class="string">'f'</span>, <span class="string">'j'</span>)]</span><br><span class="line">((<span class="string">'a'</span>, <span class="string">'d'</span>, <span class="string">'h'</span>), (<span class="string">'b'</span>, <span class="string">'e'</span>, <span class="string">'i'</span>), (<span class="string">'c'</span>, <span class="string">'f'</span>, <span class="string">'j'</span>))</span><br></pre></td></tr></table></figure>

<p>迭代的时候使用zip</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i,j,k <span class="keyword">in</span> zip(L1,L2,L3):</span><br><span class="line">    print(i,j,k)</span><br></pre></td></tr></table></figure>

<p>enumerate特殊的打包，在迭代时绑定元素的遍历序号：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">L = list(<span class="string">'abcd'</span>)</span><br><span class="line"><span class="keyword">for</span> index,value <span class="keyword">in</span> enumerate(L):</span><br><span class="line">    print(index,value)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="keyword">for</span> index,value <span class="keyword">in</span> zip(range(len(L)),L):</span><br><span class="line">    print(index,value)</span><br></pre></td></tr></table></figure>

<p>两个列表建立字典映射时，可用zip对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dict(zip(L1,L2))</span><br></pre></td></tr></table></figure>

<p>解压：zip和*配合</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">zipped = list(zip(L1,L2,L3))</span><br><span class="line">print(zipped)</span><br><span class="line">list(zip(*zipped))</span><br></pre></td></tr></table></figure>

<h2 id="练习1："><a href="#练习1：" class="headerlink" title="练习1："></a>练习1：</h2><p>列表推导式写矩阵乘法：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">M1 = np.random.rand(<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line">M2 = np.random.rand(<span class="number">3</span>,<span class="number">4</span>)</span><br><span class="line">ans = [[sum([M1[i][k] * M2[k,j] <span class="keyword">for</span> k <span class="keyword">in</span> range(M1.shape[<span class="number">1</span>])]) <span class="keyword">for</span> j <span class="keyword">in</span> range(M2.shape[<span class="number">1</span>])]<span class="keyword">for</span> i <span class="keyword">in</span> range(M1.shape[<span class="number">0</span>])]</span><br><span class="line"></span><br><span class="line">ans1 = np.dot(M1,M2)</span><br><span class="line">print(ans)</span><br><span class="line">print(ans1)</span><br></pre></td></tr></table></figure>





<h2 id="文件读写"><a href="#文件读写" class="headerlink" title="文件读写"></a>文件读写</h2><p><code>read_csv()  read_txt()   read_excel()</code>等，参数：</p>
<p><code>header=None</code>：第一行不做列名</p>
<p><code>index_col</code>：表示把某一列或几列作为索引</p>
<p><code>usecols</code>：读取列的集合，默认读所有列</p>
<p><code>parse_dates</code>：表示需要转换为时间的列</p>
<p><code>nrows</code>：表示读取的行数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pd.read_csv(<span class="string">'my_csv.csv'</span>,index_col=[<span class="string">'col1'</span>,<span class="string">'col2'</span>])</span><br><span class="line"></span><br><span class="line">pd.read_table(<span class="string">'my_table.txt'</span>,usecols=[<span class="string">'col1'</span>,<span class="string">'col2'</span>])</span><br><span class="line">pd.read_csv(<span class="string">'my_csv.csv'</span>,parse_dates=[<span class="string">'col5'</span>])</span><br></pre></td></tr></table></figure>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#读取txt文件时，遇到非空格，自定义分割符</span></span><br><span class="line"><span class="comment"># sep</span></span><br><span class="line">pd.read_table(<span class="string">'my_table_special_sep.txt'</span>,sep=<span class="string">'\|\|\|\|'</span>,engine=<span class="string">'python'</span>)</span><br></pre></td></tr></table></figure>

<p>数据写入</p>
<p>​    常用的操作是把index设置为False,特别当索引没有特殊意义的时候</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df_csv.to_csv(<span class="string">'my_csv_saved.csv'</span>,index=<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>

<p>​    python中没有<code>to_table()</code>但是<code>to_csv</code>可以保存txt文件，<code>sep</code>指定分割符</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df_txt.to_csv(<span class="string">'../data/my_txt_saved.txt'</span>, sep=<span class="string">'\t'</span>, index=<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>

<h2 id="常用函数"><a href="#常用函数" class="headerlink" title="常用函数"></a>常用函数</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">df = pd.read_csv(<span class="string">'learn_pandas.csv'</span>)</span><br><span class="line">df.columns</span><br><span class="line"><span class="comment">#取前7列</span></span><br><span class="line">df = df[df.columns[:<span class="number">7</span>]]</span><br><span class="line">df</span><br></pre></td></tr></table></figure>

<h3 id="汇总函数"><a href="#汇总函数" class="headerlink" title="汇总函数"></a>汇总函数</h3><p><code>head</code> 、<code>tail</code>、<code>info()</code>、<code>describe()</code></p>
<p><code>info</code>和<code>describe</code>返回表中信息概况和对应主要统计量，</p>
<p>如果想对一份数据集进行全面而有效的观察，特别是在列较多的情况下，推荐使用pandas-profiling包</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">df = pd.read_csv(<span class="string">'learn_pandas.csv'</span>)</span><br><span class="line">df.info()</span><br><span class="line">df.describe()</span><br></pre></td></tr></table></figure>

<h3 id="特征统计函数"><a href="#特征统计函数" class="headerlink" title="特征统计函数"></a>特征统计函数</h3><p><code>mean  sum  median  var  std  max  min</code></p>
<p><code>quantile，count，indxmax</code>:分位数、非缺失值个数、最大值对应索引</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">df_demo = df[[<span class="string">'Height'</span>,<span class="string">'Weight'</span>]]</span><br><span class="line">print(df_demo.quantile(<span class="number">0.75</span>))</span><br><span class="line">print(df_demo.count())</span><br><span class="line">print(df_demo.idxmax())</span><br></pre></td></tr></table></figure>

<h3 id="唯一值函数"><a href="#唯一值函数" class="headerlink" title="唯一值函数"></a>唯一值函数</h3><p><code>unique</code>和<code>nunique</code>可以分别得到其唯一值组成的列表和唯一值的个数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">print(df[<span class="string">'School'</span>].unique())</span><br><span class="line">print(df[<span class="string">'School'</span>].nunique())</span><br></pre></td></tr></table></figure>

<p><code>value_counts</code>可以得到<strong>唯一值</strong>和其对应出现的<strong>频数</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">df[<span class="string">'School'</span>].value_counts()</span><br></pre></td></tr></table></figure>

<p>如果想要观察多个列组合的唯一值，可以使用<code>drop_duplicates</code>。其中的关键参数是<code>keep</code>，默认值<code>first</code>表示每个组合保留第一次出现的所在行，<code>last</code>表示保留最后一次出现的所在行，<code>False</code>表示把所有重复组合所在的行剔除。 </p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">df_demo = df[[<span class="string">'Gender'</span>,<span class="string">'Transfer'</span>,<span class="string">'Name'</span>]]</span><br><span class="line">df_demo.drop_duplicates([<span class="string">'Gender'</span>,<span class="string">'Transfer'</span>])</span><br><span class="line"></span><br><span class="line">df_demo.drop_duplicates([<span class="string">'Gender'</span>, <span class="string">'Transfer'</span>], keep=<span class="string">'last'</span>)</span><br><span class="line"></span><br><span class="line">df_demo.drop_duplicates([<span class="string">'Name'</span>, <span class="string">'Gender'</span>], keep=<span class="literal">False</span>).head() <span class="comment"># 保留只出现过一次的性别和姓名组合</span></span><br></pre></td></tr></table></figure>

<h3 id="替换函数"><a href="#替换函数" class="headerlink" title="替换函数"></a>替换函数</h3><p>​    一般而言，替换操作是针对某一个列进行的，因此下面的例子都以<code>Series</code>举例。<code>pandas</code>中的替换函数可以归纳为三类：映射替换、逻辑替换、数值替换。其中映射替换包含<code>replace</code>方法、第八章中的<code>str.replace</code>方法以及第九章中的<code>cat.codes</code>方法，此处介绍<code>replace</code>的用法。</p>
<p>​    在<code>replace</code>中，可以通过字典构造，或者传入两个列表来进行替换：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">df[<span class="string">'Gender'</span>].replace(&#123;<span class="string">'Female'</span>:<span class="number">0</span>,<span class="string">'Male'</span>:<span class="number">1</span>&#125;).head()</span><br><span class="line">df[<span class="string">'Gender'</span>].replace([<span class="number">0</span>,<span class="number">1</span>],[<span class="string">'Female'</span>,<span class="string">'Male'</span>]).head()</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      
      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
        
      </div>

      <footer class="post-footer">



        
<div>    
 
 
<ul class="post-copyright">
  <li class="post-copyright-author">
      <strong>本文作者：</strong>付文杰
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="/2021/03/14/pandas/" title="Pandas学习笔记01">2021/03/14/pandas/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权： </strong>
    本站文章均采用 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0 CN</a> 许可协议，请勿用于商业，转载注明出处！
  </li>
</ul>

</div>
          <div class="post-tags">
              <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" rel="tag"><i class="fa fa-tag"></i> 学习笔记</a>
              <a href="/tags/Python/" rel="tag"><i class="fa fa-tag"></i> Python</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2021/03/13/numpy/" rel="next" title="Numpy学习笔记">
                  <i class="fa fa-chevron-left"></i> Numpy学习笔记
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2021/03/27/pandas02/" rel="prev" title="Pandas学习笔记02">
                  Pandas学习笔记02 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=27588222&auto=1&height=66"></iframe>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Python基础部分"><span class="nav-number">1.</span> <span class="nav-text">Python基础部分</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#列表推导式和条件赋值"><span class="nav-number">1.1.</span> <span class="nav-text">列表推导式和条件赋值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#匿名函数和map方法"><span class="nav-number">1.2.</span> <span class="nav-text">匿名函数和map方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#zip对象和enumerate方法"><span class="nav-number">1.3.</span> <span class="nav-text">zip对象和enumerate方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#练习1："><span class="nav-number">1.4.</span> <span class="nav-text">练习1：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件读写"><span class="nav-number">1.5.</span> <span class="nav-text">文件读写</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用函数"><span class="nav-number">1.6.</span> <span class="nav-text">常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#汇总函数"><span class="nav-number">1.6.1.</span> <span class="nav-text">汇总函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#特征统计函数"><span class="nav-number">1.6.2.</span> <span class="nav-text">特征统计函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#唯一值函数"><span class="nav-number">1.6.3.</span> <span class="nav-text">唯一值函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#替换函数"><span class="nav-number">1.6.4.</span> <span class="nav-text">替换函数</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="付文杰"
    src="/images/headImage.jpg">
  <p class="site-author-name" itemprop="name">付文杰</p>
  <div class="site-description" itemprop="description">兰州交通大学 | 本科生 <br> 信息管理与信息系统</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">63</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/FWj1635387072" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;FWj1635387072" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:1635387072@qq.com" title="E-Mail &amp;rarr; mailto:1635387072@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/null" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>





      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        


<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">付文杰</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">210k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:50</span>

  <br>
  <span id="sitetime"></span>

</div>
<script language=javascript>
    function siteTime() {
        window.setTimeout("siteTime()", 1000);
        var seconds = 1000;
        var minutes = seconds * 60;
        var hours = minutes * 60;
        var days = hours * 24;
        var years = days * 365;
        var today = new Date();
        var todayYear = today.getFullYear();
        var todayMonth = today.getMonth() + 1;
        var todayDate = today.getDate();
        var todayHour = today.getHours();
        var todayMinute = today.getMinutes();
        var todaySecond = today.getSeconds();
        /* Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
        year - 作为date对象的年份，为4位年份值
        month - 0-11之间的整数，做为date对象的月份
        day - 1-31之间的整数，做为date对象的天数
        hours - 0(午夜24点)-23之间的整数，做为date对象的小时数
        minutes - 0-59之间的整数，做为date对象的分钟数
        seconds - 0-59之间的整数，做为date对象的秒数
        microseconds - 0-999之间的整数，做为date对象的毫秒数 */
        var t1 = Date.UTC(2019, 11, 7, 17, 50, 00); //北京时间2018-2-13 00:00:00
        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
        var diff = t2 - t1;
        var diffYears = Math.floor(diff / years);
        var diffDays = Math.floor((diff / days) - diffYears * 365);
        var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
        var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) / minutes);
        var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours - diffMinutes * minutes) / seconds);
        document.getElementById("sitetime").innerHTML = "本站已运行 " +diffYears+" 年 "+diffDays + " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
    }/*因为建站时间还没有一年，就将之注释掉了。需要的可以取消*/
    siteTime();
</script>


        












        
      </div>
    </footer>
  </div>

  
  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  
















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"symbols_count_time":null,"symbols":true,"time":true,"total_symbols":true,"total_time":true,"log":false});</script></body>
</html>
<script type="text/javascript" src="/js/src/clicklove.js"></script>